package cn.bdqn.asecu.controller;

import cn.bdqn.asecu.service.AsecuUser_Service;
import cn.bdqn.asecu.utility.Utility_project;
import cn.bdqn.pojo.Operationlog;
import cn.bdqn.pojo.Project;
import cn.bdqn.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

//模块一
/*
 * 登录
 *
 *
 * */
@Controller
public class AsecuController {

    int i=0;

    @Resource
    private AsecuUser_Service asecuUser_service;

    //默认登录页面
    @RequestMapping("/")
    public  String registeryem(){
        return "login";
    }

    @RequestMapping("/login")
    public  String login(){
        return "login";
    }
    //首页
    @RequestMapping("index")
    public  String homepage(){
        return "index";
    }

    //登录验证
    @RequestMapping("verify_login")
    @ResponseBody
    public String verify_login(HttpSession session,String name, String password){
        User user = asecuUser_service.loing_verify(name, password);
        if (user!=null){
            if (user.getState().equals("0")) {
                session.setAttribute("user", user);
                Operationlog operationlog = new Operationlog(name, new Date(), user.getQualification(), "系统", "登录", "成功");
                asecuUser_service.addlog(operationlog);
                return "true";
            }else{
                return "该用户过期!";
            }
        }
        Operationlog operationlog=new Operationlog(name,new Date(),"","系统","登录","失败");
        asecuUser_service.addlog(operationlog);
        return "false";

    }

    //验证获取码
    @RequestMapping("verifys")
    @ResponseBody
    public  List<String>  verify(){
        List<String> list =new ArrayList<>();
        list.add(Show_name());
        return list;
    }

    //随机验证码
    public String Show_name(){
        //定义一个字符串（A-Z，a-z，0-9）即62位；
        String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        //由Random生成随机数
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        //长度为几就循环几次
        for(int i=0; i<5; ++i){
            //产生0-61的数字
            int number=random.nextInt(62);
            //将产生的数字通过length次承载到sb中
            sb.append(str.charAt(number));
        }

        return sb.toString();
    }

    //退出
    @RequestMapping("quitym")
    public  String logout(HttpSession session){
        User user = (User) session.getAttribute("user");
        Operationlog operationlog = new Operationlog(user.getName(), new Date(), user.getQualification(), "系统", "", "退出");
        asecuUser_service.addlog(operationlog);
        session.removeAttribute("user");
        return "login";
    }

    //我的桌面
    @RequestMapping("welcome")
    public ModelAndView Mydesktop(){
        i++;
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("i",i);
        modelAndView.setViewName("welcome");
        return modelAndView;
    }

    /*    我的平台*/

    //待办事项页面
    @RequestMapping("article-list")
    public  String article(){
        return "article-list";
    }

    //待办事项
    @RequestMapping("backlogShow")
    @ResponseBody
    public List<Utility_project> backlog(HttpSession session, Integer type){
        User user = (User) session.getAttribute("user");
        List<Utility_project> utility_projects = asecuUser_service.backlogSel(user.getId(), type, user.getRoleId());
        return utility_projects;
    }

    @RequestMapping("dispose")
    public String dispose(Integer[] items_id,HttpSession session){
        if(items_id!=null) {
            User user = (User) session.getAttribute("user");
            for (Integer i : items_id) {
                int i1 = asecuUser_service.disposeUpl(i, user.getName());
            }
        }
        return "article-list";
    }


    @RequestMapping("article-list1")
    public String articlelist1(){
        return "article-list1";
    }

    @RequestMapping("articlelist1_Show")
    @ResponseBody
    public List<Project> articlelist1_Show(){
        return asecuUser_service.project_zt();
    }


    @RequestMapping("lingdao1")
    public  String lingdao1(){
        return "lingdao1";
    }

    @RequestMapping("lingda1_Show")
    @ResponseBody
    public List<Utility_project> lingda1_Show(){
        List<Utility_project> selproject = asecuUser_service.Selproject();
        return selproject;
    }

    @RequestMapping("lingdao2")
    public String lingdao2(){
        return "lingdao2";
    }

    @RequestMapping("lingdaoShow")
    @ResponseBody
    public List<Utility_project>  lingdao2_Show(){
        List<Utility_project> selproject = asecuUser_service.Selproject();
        return selproject;
    }

}
